<template>
  <div>
    <el-form :model="ruleForm" label-width="120px" class="demo-ruleForm">
      <el-form-item label="案例类型" prop="type">
        <span>{{ typelabel(ruleForm.type) }}</span>
      </el-form-item>
      <el-form-item label="案例名称" prop="name">
        <span v-html="ruleForm.name" />
      </el-form-item>
      <el-form-item label="能力训练类型" prop="abilityType">
        <span>{{ abilityType.name }}</span>
      </el-form-item>
      <el-form-item label="难度等级">
        <el-rate v-model="ruleForm.level" disabled style="margin-top:8px;" />
      </el-form-item>
      <el-form-item label="创始人">
        <el-tag v-for="item in teacher" :key="item.id">{{ item.name }}&nbsp;</el-tag>
      </el-form-item>
      <el-form-item v-if="ruleForm.type === 3" label="视频解答">
        <el-tag>{{ ruleForm.videoSolution?'是':'否' }}</el-tag>
      </el-form-item>
      <el-form-item v-else label="翻转案例">
        <el-tag>{{ ruleForm.flip?'是':'否' }}</el-tag>
      </el-form-item>
      <el-form-item label="标准视频" prop="standardVideo">
        <span v-html="ruleForm.standardVideo" />
      </el-form-item>
      <el-form-item label="案例详情">
        <div v-html="ruleForm.description" />
      </el-form-item>
      <template v-if="editid!==0">
        <el-form-item label="OKR">
          <vue2-org-tree
            name="test"
            :data="okr"
            :props="{id: 'id',label: 'name', children: 'list' }"
            :horizontal="horizontal"
            :collapsable="collapsable"
            :label-class-name="labelClassName"
            :render-content="renderContent"
          />
        </el-form-item>
        <el-form-item>
          <el-table
            :data="table"
            :span-method="objectSpanMethod"
            border
          >
            <el-table-column prop="Objectives" label="Objectives" :width="width" />
            <el-table-column prop="OScore" label="Score" :width="width" />
            <el-table-column prop="KeyResults" label="Key Results" :width="width" />
            <el-table-column prop="KScore" label="Score" :width="width" />
            <el-table-column prop="Capability" label="Capability" :width="width" />
          </el-table>
        </el-form-item>
      </template>
    </el-form>
  </div>
</template>

<script>
import TreeMenu from '@/components/TreeMenu'
import caseutil from '@/views/content/case/caseutil'

export default {
  name: 'CaseDetail',
  data() {
    return {
      width: '150px',
      ruleForm: {
        id: '',
        name: '',
        type: 1,
        description: '',
        abilityType: '',
        level: 1,
        createId: '',
        videoSolution: true,
        standardVideo: '',
        flip: true
      },
      okr: {},
      editid: 0,
      teacher: '',
      horizontal: true,
      collapsable: false,
      expandAll: true,
      labelClassName: 'bg-white',
      types: [],
      table: [],
      abilityType: '',
      merge: {}
    }
  },
  created() {
    this.types = this.$store.getters.casetype

    // this.editid = this.$store.getters.params.id
    this.editid = this.$store.getters.params.id || 0
    if (this.editid !== 0) {
      this.ruleForm.id = this.editid
      this.init()
    }
  },
  updated() {
    Prism.highlightAll()
  },
  methods: {
    ...caseutil,
    init() {
      this.get('/neueducase/getone', response => {
        this.ruleForm = response.data.neueducase
        this.okr = response.data.okr
        this.table = this.getTable(this.okr.list)
        this.getTeacher(response.data.neueducase.createId)
        this.getAbilityType(this.ruleForm.abilityType)
      }, { id: this.editid })
    },
    renderContent(h, data) {
      return h(TreeMenu, {
        props: {
          obj: data,
          callback: this.init
        }
      })
    },
    getTeacher(id) {
      this.get('/teacher/getByIds', response => {
        this.teacher = response.data
      }, { 'ids': id })
    },
    getAbilityType(id) {
      this.get('/ability-type/getone', response => {
        this.abilityType = response.data
      }, { id: id })
    },
    typelabel(typeid) {
      for (let i = 0; i < this.$store.getters.casetype.length; i++) {
        if (typeid === this.$store.getters.casetype[i].value) {
          return this.$store.getters.casetype[i].label
        }
      }
    }
  }
}
</script>
<style scoped>
  .org-tree-node-label {
    white-space: nowrap;
  }
  .bg-white {
    background-color: white;
  }
  .bg-orange {
    background-color: orange;
  }
  .bg-gold {
    background-color: gold;
  }
  .bg-gray {
    background-color: gray;
  }
  .bg-lightpink {
    background-color: lightpink;
  }
  .bg-chocolate {
    background-color: chocolate;
  }
  .bg-tomato {
    background-color: tomato;
  }
</style>
